<template>
  <LoginCheck>
    <div class="profile">
      <!-- 用户信息 -->
      <div class="user-info">
        <div class="avatar">
          <img :src="user.avatar_url || '/images/avatars/user1_avatar.jpg'" :alt="user.nickname" />
        </div>
        <div class="info">
          <h3>{{ user.nickname }}</h3>
          <p>{{ user.phone }}</p>
        </div>
      </div>

      <!-- 功能菜单 -->
      <div class="section">
        <van-cell-group>
          <van-cell title="消息通知" is-link @click="goToNotifications" />
          <van-cell title="意见反馈" is-link @click="goToFeedback" />
          <van-cell title="关于我们" is-link @click="goToAbout" />
          <van-cell title="设置" is-link @click="goToSettings" />
        </van-cell-group>
      </div>
    </div>
  </LoginCheck>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { showToast } from 'vant'
import LoginCheck from '@/components/LoginCheck.vue'

// 用户资料数据类型定义
interface UserProfile {
  id: number
  nickname: string
  avatar_url: string
  phone: string
  created_at: string
}

const router = useRouter()
const authStore = useAuthStore()

// 用户信息
const user = ref<UserProfile>({
  id: 0,
  nickname: '',
  avatar_url: '',
  phone: '',
  created_at: ''
})

const loading = ref(false)

// 获取用户资料
const fetchUserProfile = async () => {
  if (!authStore.userInfo?.phone) {
    showToast('请先登录')
    return
  }

  loading.value = true
  try {
    const response = await fetch(`/api/user/profile?phone=${authStore.userInfo.phone}`)
    
    if (!response.ok) {
      throw new Error('获取用户资料失败')
    }
    
    const data = await response.json()
    user.value = data
  } catch (error) {
    console.error('获取用户资料错误:', error)
    showToast('获取用户资料失败')
  } finally {
    loading.value = false
  }
}

// 跳转到消息通知
const goToNotifications = () => {
  console.log('跳转到消息通知')
  // TODO: 实现消息通知页面
}

// 跳转到意见反馈
const goToFeedback = () => {
  console.log('跳转到意见反馈')
  // TODO: 实现意见反馈页面
}

// 跳转到关于我们
const goToAbout = () => {
  console.log('跳转到关于我们')
  // TODO: 实现关于我们页面
}

// 跳转到设置
const goToSettings = () => {
  console.log('跳转到设置')
  // TODO: 实现设置页面
}

// 页面加载时获取数据
onMounted(() => {
  fetchUserProfile()
})
</script>

<style scoped>
.profile {
  background: #f5f5f5;
  min-height: 100vh;
  padding-bottom: 20px; /* 额外底部边距，确保内容不被tabbar遮挡 */
}

.user-info {
  background: white;
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 10px;
}

.avatar img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
}

.info h3 {
  margin: 0 0 5px 0;
  font-size: 18px;
}

.info p {
  margin: 0;
  color: #666;
}

.section {
  background: white;
  margin-bottom: 10px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
}

.section-header h3 {
  margin: 0;
  font-size: 16px;
}

.wallet-info {
  padding: 20px;
}

.balance, .cards {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.balance:last-child, .cards:last-child {
  margin-bottom: 0;
}

.label {
  color: #666;
}

.amount {
  color: #ff6b6b;
  font-size: 18px;
  font-weight: bold;
}

.count {
  color: #1890ff;
  font-weight: bold;
}
</style> 